package com.cn.mmd.controller;

import com.cn.mmd.model.*;
import com.cn.mmd.service.*;
import com.cn.mmd.tools.SplitSpring;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 老师控制成绩
 */

@Controller
@RequestMapping(value = "page/grade")
public class GradeController {

    @Autowired
    private GradeService gradeService;

    @Autowired
    private StudentService studentService;

    @Autowired
    private StudentAppService studentAppService;

    @Autowired
    private CourseService courseService;

    @Autowired
    private TokenController tokenController;

    SplitSpring splitSpring = new SplitSpring();

    @RequestMapping(value = "/select")
    public synchronized
    @ResponseBody
    Object selectGrade(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        List<Map> mapList = new ArrayList<Map>();
        String courseId = request.getParameter("courseId");
        String teacherId = request.getParameter("teacherId");
//        Integer startrow = Integer.parseInt(request.getParameter("startrow"));
//        Integer pageSize = Integer.parseInt(request.getParameter("pageSize"));
        String token = request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
//        System.out.println(courseId+","+teacherId+","+startrow+","+pageSize);
        if (teacherData != null) {
//            startrow = (startrow - 1) * pageSize;
            Map<String, Object> map;
            Grade grade = new Grade();
            grade.setTeacherid(teacherId);
            grade.setCoursespuid(courseId);
//            grade.setStartrow(startrow);
//            grade.setPageSize(pageSize);
            if (grade != null) {
                List<Grade> gradeList = gradeService.selectByCouIdStuIdTeaId(grade);
                if (gradeList != null && gradeList.size() > 0) {
                    StudentApp studentApp;
                    for (int i = 0; i < gradeList.size(); i++) {
                        map = new HashMap<String, Object>();
                        grade = gradeList.get(i);
                        if (grade != null) {
                            map.put("gradeId", grade.getGradeid() + "");
                            map.put("stuId", grade.getStuid());
                            studentApp = studentAppService.selectByPrimaryKey(grade.getStuid());
                            map.put("studentUrl", studentApp.getStudentheadportrait());
                            map.put("studentName", studentApp.getStudentname());
                            map.put("classRoomGrade", grade.getClassroomggrade() + "");
                            map.put("innovateGrade", grade.getInnovategrade() + "");
                            mapList.add(map);
                        }
                    }
                    Course course = new Course();
                    course.setCourseid(courseId);
                    course = courseService.selectClass(course);
//                        int num = pageSize - gradeList.size();
                    if (course != null) {
                        List<Student> student;
                        Student student1;
                        Grade grade1;
                        if (splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0).length > 0) {
                            for (int i = 0; i < splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0).length; i++) {
                                student = studentService.selectByClass(splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0)[i]);
                                for (int j = 0; j < student.size(); j++) {
                                    map = new HashMap<String, Object>();
                                    student1 = student.get(j);
                                    grade1 = new Grade();
                                    grade1.setStuid(student1.getStudentid());
                                    grade1.setCoursespuid(courseId);
                                    grade1 = gradeService.selectByStuId(grade1);
                                    if (grade1 == null) {
                                        map.put("gradeId", 0 + "");
                                        map.put("stuId", student1.getStudentid());
                                        studentApp = studentAppService.selectByPrimaryKey(student1.getStudentid());
                                        map.put("studentUrl", studentApp.getStudentheadportrait());
                                        map.put("studentName", studentApp.getStudentname());
                                        map.put("classRoomGrade", 0 + "");
                                        map.put("innovateGrade", 0 + "");
                                        mapList.add(map);
//                                            num--;
//                                            if (num==0){
//                                                i = splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0).length;
//                                                break;
//                                            }
                                    }
                                }
                            }
                        }
                    }
                } else {
                    StudentApp studentApp;
                    Course course = new Course();
                    course.setCourseid(courseId);
                    course = courseService.selectClass(course);
                    if (course != null) {
                        List<Student> student;
                        Student student1;
                        if (splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0).length > 0) {
                            int num = 0;
                            for (int i = 0; i < splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0).length; i++) {
                                student = studentService.selectByClass(splitSpring.splitSpringBy(course.getCourseclasss(), 0, 0, ",", 0)[i]);
                                for (int j = 0; j < student.size(); j++) {
//                                    if (num==pageSize){
//                                        break;
//                                    }
                                    map = new HashMap<String, Object>();
                                    student1 = student.get(j);
                                    map.put("gradeId", 0 + "");
                                    map.put("stuId", student1.getStudentid());
                                    studentApp = studentAppService.selectByPrimaryKey(student1.getStudentid());
                                    map.put("studentUrl", studentApp.getStudentheadportrait());
                                    map.put("studentName", studentApp.getStudentname());
                                    map.put("classRoomGrade", 0 + "");
                                    map.put("innovateGrade", 0 + "");
                                    mapList.add(map);
                                    num++;
                                }
                            }
                        }
                    }
                }
                if (mapList != null && mapList.size() > 0) {
                    objectMap.put("data", mapList);
                    objectMap.put("message", "成功");
                    objectMap.put("code", 200);
                } else {
                    objectMap.put("data", mapList);
                    objectMap.put("message", "失败");
                    objectMap.put("code", 201);
                }
            } else {
                objectMap.put("message", "请传入参数");
                objectMap.put("code", 500);
            }
        } else {
            objectMap.put("message", "此用户没有权限做此操作");
            objectMap.put("code", 203);
        }
        return objectMap;
    }

    /**
     * 查看单人成绩
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectOne")
    public synchronized
    @ResponseBody
    Object selectOneGrade(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        String courseId = request.getParameter("courseId");
        String teacherId = request.getParameter("teacherId");
        String token = request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData != null) {
            Student student;
            StudentApp studentApp;
            student = studentService.selectByPrimaryKey(studentId);
            if (student != null) {
                studentApp = studentAppService.selectByPrimaryKey(studentId);
                if (studentApp != null) {
                    objectMap.put("studentId", student.getStudentid());
                    objectMap.put("studentClass", student.getStudentclass());
                    objectMap.put("studentMajor", student.getStudentmajor());
                    objectMap.put("commentNum", studentApp.getCommentnum() + "");
                    objectMap.put("viewedNum", studentApp.getViewednum() + "");
                    objectMap.put("studentUrl", studentApp.getStudentheadportrait());
                    if (studentApp.getMynickname() != null && !studentApp.getMynickname().equals("")) {
                        objectMap.put("studentName", studentApp.getMynickname());
                    } else {
                        objectMap.put("studentName", studentApp.getStudentname());
                    }
                    Grade grade = new Grade();
                    grade.setTeacherid(teacherId);
                    grade.setStuid(studentId);
                    grade.setCoursespuid(courseId);
                    if (gradeService.selectByCouIdStuIdTeaIdo(grade) != null) {
                        grade = gradeService.selectByCouIdStuIdTeaIdo(grade);
                        objectMap.put("gradeId", grade.getGradeid() + "");
                        objectMap.put("classroomGrade", grade.getClassroomggrade() + "");
                        objectMap.put("innovateGrade", grade.getInnovategrade() + "");
                    } else {
                        objectMap.put("gradeId", 0 + "");
                        objectMap.put("classroomGrade", 0 + "");
                        objectMap.put("innovateGrade", 0 + "");
                    }
                }
                if (objectMap != null) {
                    if (objectMap.size() > 0) {
                        objectMap.put("code", 200);
                        objectMap.put("message", "成功");
                    } else {
                        objectMap.put("code", 202);
                        objectMap.put("message", "数据返回失败");
                    }
                } else {
                    objectMap.put("code", 202);
                    objectMap.put("message", "数据返回失败");
                }
            } else {
                objectMap.put("code", 201);
                objectMap.put("message", "这个学生没有成绩");
            }
        } else {
            objectMap.put("code", 203);
            objectMap.put("message", "此用户没有权限做此操作");
        }
        return objectMap;
    }


    /**
     * 修改单人成绩
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/updOne")
    public synchronized
    @ResponseBody
    Object updOneGrade(HttpServletRequest request) {
        Map<String, Object> objectMap = new HashMap<String, Object>();
        String studentId = request.getParameter("studentId");
        String courseId = request.getParameter("courseId");
        String teacherId = request.getParameter("teacherId");
        Integer gradeId = Integer.parseInt(request.getParameter("gradeId"));//暂定是由前面直接获取还是通过语句精确查找
        Float classroomGrade = Float.valueOf(request.getParameter("classroomGrade"));
        Float innovateGrade = Float.valueOf(request.getParameter("innovateGrade"));
        String token = request.getParameter("token");
        TeacherData teacherData;
        teacherData = (TeacherData) tokenController.token(teacherId, token);
        if (teacherData != null) {
            Grade grade;
            grade = new Grade();
            grade.setTeacherid(teacherId);
            grade.setStuid(studentId);
            grade.setCoursespuid(courseId);
            grade = gradeService.selectByCouIdStuIdTeaIdo(grade);
            if (grade != null) {
                gradeId = grade.getGradeid();
            }
            Grade grade1 = gradeService.selectByPrimaryKey(gradeId);
            System.out.println(grade1 + "," + gradeId);
            if (grade1 != null) {
                grade = new Grade();
                Float totalGradeOut = classroomGrade + innovateGrade + grade1.getTotalgrade();
                grade.setClassroomggrade(classroomGrade + grade1.getClassroomggrade());
                grade.setInnovategrade(innovateGrade + grade1.getInnovategrade());
                grade.setTotalgrade(totalGradeOut);
                grade.setGradeid(gradeId);
                if (gradeService.updateByPrimaryKeySelective(grade) > 0) {
                    objectMap.put("message", "成功");
                    objectMap.put("code", 200);
                } else {
                    objectMap.put("message", "查看失败");
                    objectMap.put("code", 201);
                }
            } else {
                grade = new Grade();
                grade.setStuid(studentId);
                grade.setCoursespuid(courseId);
                grade.setTeacherid(teacherId);
                grade.setClassroomggrade(classroomGrade);
                grade.setInnovategrade(innovateGrade);
                grade.setTotalgrade(classroomGrade + innovateGrade);
                if (gradeService.insertSelective(grade) > 0) {
                    objectMap.put("message", "成功");
                    objectMap.put("code", 201);
                } else {
                    objectMap.put("message", "查看失败");
                    objectMap.put("code", 201);
                }
            }
        } else {
            objectMap.put("message", "此用户没有权限做此操作");
            objectMap.put("code", 202);
        }
        return objectMap;
    }
}
